<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Payment_history{
	var $current_dbprefix = "";
	var $CI;
	var $id = 0;
	var $escort_id = 0;
	var $user_id = 0;
	var $payment_ids = '';
	var $info = '';
	var $datetime = 0;
	var $total_price = 0;
	var $promotion_code = '';
	var $discount_unit = '';
	var $discount_amount = '';
	var $net_amount = '';
	var $escorts_apply_number = 0;
	var $start_date = 0;
	var $end_date = 0;
	var $period = 0;
	var $period_discount_amount = 0;
	var $volumn_discount = 0;
	var $volumn_discount_unit = '';
	var $payment_status = '';
	
	function __construct() {
		$this->CI= & get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $this->CI->db->where('id',$input)->get(TBL_PAYMENT_HISTORY)->result();
		}
		else
		{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->escort_id = $result[0]->escort_id;
			$this->user_id = $result[0]->user_id;
			$this->payment_ids = $result[0]->payment_ids;
			$this->info = $result[0]->info;
			$this->datetime = $result[0]->datetime;
			$this->total_price = $result[0]->total_price;
			$this->promotion_code = $result[0]->promotion_code;
			$this->discount_unit = $result[0]->discount_unit;
			$this->discount_amount = $result[0]->discount_amount;
			$this->net_amount = $result[0]->net_amount;
			$this->escorts_apply_number = $result[0]->escorts_apply_number;
			$this->start_date = $result[0]->start_date;
			$this->end_date = $result[0]->end_date;
			$this->period = $result[0]->period;
			$this->period_discount_amount = $result[0]->period_discount_amount;
			$this->volumn_discount = $result[0]->volumn_discount;
			$this->volumn_discount_unit = $result[0]->volumn_discount_unit;
			$this->payment_status = $result[0]->payment_status;
			
			return $this;
		}
		return false;
	}
	
	function setEscortId($input){$this->escort_id=$input;}
	function setUserId($input){$this->user_id=$input;}
	function setPaymentIds($input){$this->payment_ids=$input;}	 
	function setInfo($input){$this->info=$input;}
	function setDateTime($input){$this->datetime=$input;}
	function setTotalPrice($input){$this->total_price=$input;}
	function setPromotionCode($input){$this->promotion_code=$input;}
	function setDiscountUnit($input){$this->discount_unit=$input;}
	function setDiscountAmount($input){$this->discount_amount=$input;}
	function setNetAmount($input){$this->net_amount=$input;}
	function setEscortsApplyNumber($input){$this->escorts_apply_number=$input;}
	function setStartDate($input){ $this->start_date=$input; }
	function setEndDate($input){ $this->end_date=$input; }
	function setPeriod($input){ $this->period=$input; }
	function setPeriodDiscountAmount($input){ $this->period_discount_amount=$input; }
	function setVolumnDiscount($input){ $this->volumn_discount=$input; }
	function setVolumnDiscountUnit($input){ $this->volumn_discount_unit=$input; }
	function setPaymentStatus($input){$this->payment_status=$input;}
	
	function getId(){return $this->id;}
	function getEscortId(){return $this->escort_id;}
	function getUserId(){return $this->user_id;}
	function getPaymentIds(){return $this->payment_ids;}	 
	function getInfo(){return $this->info;}
	function getDateTime(){return $this->datetime;}
	function getTotalPrice(){return $this->total_price;}
	function getPromotionCode(){return $this->promotion_code;}
	function getDiscountUnit(){return $this->discount_unit;}
	function getDiscountAmount(){return $this->discount_amount;}
	function getNetAmount(){return $this->net_amount;}
	function getEscortsApplyNumber(){return $this->escorts_apply_number;}
	function getStartDate(){ return  $this->start_date; }
	function getEndDate(){ return $this->end_date; }
	function getPeriod(){ return $this->period; }
	function getPeriodDiscountAmount(){ return $this->period_discount_amount; }
	function getVolumnDiscount(){ return $this->volumn_discount; }
	function getVolumnDiscountUnit(){ return $this->volumn_discount_unit; }
	function getPaymentStt(){ return $this->payment_status;}
	
	function update()
	{
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('escort_id', $this->escort_id);
		$this->CI->db->set('user_id', $this->user_id);
		$this->CI->db->set('payment_ids', $this->payment_ids);
		$this->CI->db->set('info', $this->info);
		$this->CI->db->set('datetime', $this->datetime);
		$this->CI->db->set('total_price', $this->total_price);
		$this->CI->db->set('promotion_code', $this->promotion_code);
		$this->CI->db->set('discount_unit', $this->discount_unit);
		$this->CI->db->set('discount_amount', $this->discount_amount);
		$this->CI->db->set('net_amount', $this->net_amount);
		$this->CI->db->set('escorts_apply_number', $this->escorts_apply_number);
		$this->CI->db->set('start_date', $this->start_date);
		$this->CI->db->set('end_date', $this->end_date);
		$this->CI->db->set('period', $this->period);
		$this->CI->db->set('period_discount_amount', $this->period_discount_amount);
		$this->CI->db->set('volumn_discount', $this->volumn_discount);
		$this->CI->db->set('volumn_discount_unit', $this->volumn_discount_unit);
		$this->CI->db->set('payment_status', $this->payment_status);
		
		$this->CI->db->where('id', $this->id)->update(TBL_PAYMENT_HISTORY);
	}
	
	function createNewEntity(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('datetime',time() );
		$this->CI->db->insert(TBL_PAYMENT_HISTORY);
		return $this->init('id',$this->CI->db->insert_id());
	} 
	
	function getLatestPaymentHistory($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$res = $this->CI->db->where('escort_id',$escort_id)->order_by('datetime','desc')->get(TBL_PAYMENT_HISTORY)->result();
		return $res ? $this->init('id',$res[0]->id): false;
	}
	
	function getPaymentHistoryOfEscort($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$res = $this->CI->db->where('escort_id',$escort_id)->order_by('datetime','desc')->get(TBL_PAYMENT_HISTORY)->result();
		return $res;
	}
	
	function getPaymentHistoryOfAgent($agent_id){
		$this->CI->db->set_dbprefix('default_');
		
		$res = $this->CI->db->where('user_id',$agent_id)->order_by('datetime','desc')->get(TBL_PAYMENT_HISTORY)->result();
		return $res;
	}
	
	function createNewInvoiceEscort(){
		$this->CI->db->set_dbprefix('default_');
		
		$ESCORT = $this->CI->escort_entity->init('id',$this->getEscortId());
		$DISCOUNT= $this->CI->discount_ticket_entity->getDiscountByPromotionCode($ESCORT->getPromotionCode());
		$payment_log_array = array_filter(explode(',',$this->getPaymentIds()));
		
		$amount = $unit = '';
		if($DISCOUNT){
			$amount = $DISCOUNT->amount;
			switch($DISCOUNT->unit){
				case 'month':
					$unit = 'MONTH';
					break;
				case '%':
					$unit = 'PERCENT';
					break;
				default:
					$unit = 'EURO';
					break;
			}
			$this->CI->db->set('used_time',1+ $DISCOUNT->used_time);
			$this->CI->db->where('discount_code',$DISCOUNT->discount_code)->update(TBL_DISCOUNT_TICKET);
		}
		
		$this->CI->db->set('process','Auto');
		$this->CI->db->set('created_time',$this->start_date);
		$this->CI->db->set('expire_date',$this->end_date);
		$this->CI->db->set('status','no');
		$this->CI->db->set('company','Escort');
		$this->CI->db->set('description','Payment Of Escort Purchase Service');
		$this->CI->db->set('history_id',$this->getId());
		$this->CI->db->set('user_id',$ESCORT->getUserId());
		$this->CI->db->insert(TBL_INVOICE);
		$invoice_id = $this->CI->db->insert_id();

		foreach($payment_log_array as $item){
			$PAY_LOG = $this->CI->payment_log_entity->init('id',$item);
			$price = $PAY_LOG->net_price;
			
			$desc ='';
			if($PAY_LOG->getServiceType() == 'SUBS'){
				$subs_array = getSubcriptionData_ioc($PAY_LOG->type_id);
				$desc = $subs_array[0];
			}
			
			if($PAY_LOG->getServiceType() == 'PLACE'){
				$placeInfo = getPlaceListDataInfoById($PAY_LOG->type_id);
				
				$desc = $placeInfo['country'];
				if($placeInfo['state']){
					$desc .= "/".$placeInfo['state'];
				}	
				 
				if($placeInfo['province']){
					$desc .= "/".$placeInfo['province'];
				}
				
				if($placeInfo['city']){
					$desc .= "/".$placeInfo['city'];
				}
				
			}
			
			if($PAY_LOG->getServiceType() == 'PACK'){
				$pack_array = getPackListData_ioc($PAY_LOG->type_id);
				$desc = $pack_array[0];
			}
			
			if($PAY_LOG->getServiceType() == 'PRI'){
				$pri_array = getPriorityData_ioc($PAY_LOG->type_id);
				$desc = $pri_array[0];
			}
			
			$this->CI->db->set('invoice_id',$invoice_id);
			$this->CI->db->set('quantity',1);
			$this->CI->db->set('price',$price);
			$this->CI->db->set('type',$PAY_LOG->getServiceType());
			$this->CI->db->set('description',$desc);
			$this->CI->db->insert(TBL_INVOICE_LINE); 
		}	
		
		$this->CI->payment_log_entity->calculate_comission($ESCORT->getUserId(),$invoice_id);
		return $invoice_id;
	}
	
	function createNewInvoiceAgent(){
		$this->CI->db->set_dbprefix('default_');
		
		$USER = getAccountUserObject();
		$user_id= $this->getUserId();
		$number_escorts = $USER->getEscortsApplyNumber();
		
		$this->CI->db->set('actual_registered',$number_escorts);
		$this->CI->db->where('agent_id',$user_id)->update(TBL_VOLUME_DISCOUNT);
		
		$this->CI->db->set('process','Auto');
		$this->CI->db->set('created_time',$this->start_date);
		$this->CI->db->set('expire_date',$this->end_date);
		$this->CI->db->set('status','no');
		$this->CI->db->set('company','Agent');
		$this->CI->db->set('description','Payment Of Agent Purchase Service');
		$this->CI->db->set('history_id',$this->getId());
		$this->CI->db->set('user_id',$user_id);
		$this->CI->db->insert(TBL_INVOICE);
		$invoice_id = $this->CI->db->insert_id();
			
		$payment_log_array = array_filter(explode(',',$this->getPaymentIds()));
		
		foreach($payment_log_array as $item){
			$PAY_LOG = $this->CI->payment_log_entity->init('id',$item);
			$price = $PAY_LOG->net_price;
			
			$desc ='';
			if($PAY_LOG->getServiceType() == 'SUBS'){
				$subs_array = getSubcriptionData_ioc($PAY_LOG->type_id);
				$desc = $subs_array[0];
			}
			
			if($PAY_LOG->getServiceType() == 'PLACE'){
				$placeInfo = getPlaceListDataInfoById($PAY_LOG->type_id);
				
				$desc = $placeInfo['country'];
				if($placeInfo['state']){
					$desc .= "/".$placeInfo['state'];
				}	
				 
				if($placeInfo['province']){
					$desc .= "/".$placeInfo['province'];
				}
				
				if($placeInfo['city']){
					$desc .= "/".$placeInfo['city'];
				}
				
			}
			
			if($PAY_LOG->getServiceType() == 'PACK'){
				$pack_array = getPackListData_ioc($PAY_LOG->type_id);
				$desc = $pack_array[0];
			}
			
			if($PAY_LOG->getServiceType() == 'PRI'){
				$pri_array = getPriorityData_ioc($PAY_LOG->type_id);
				$desc = $pri_array[0];
			}
			
			$this->CI->db->set('invoice_id',$invoice_id);
			$this->CI->db->set('quantity',1);
			$this->CI->db->set('price',$price);
			$this->CI->db->set('type',$PAY_LOG->getServiceType());
			$this->CI->db->set('description',$desc);
			$this->CI->db->insert(TBL_INVOICE_LINE); 
		}
		
		$this->CI->payment_log_entity->calculate_comission($user_id,$invoice_id);
		return $invoice_id;
	}
	
	function getPaymentStatus($history_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where('history_id',$history_id)->get(TBL_INVOICE)->result();
		return ($rs)? $rs[0]->status : false;
	}
	
	function getLastPaymentOfUser($user_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where('user_id',$user_id)->order_by('datetime','desc')->get(TBL_PAYMENT_HISTORY)->result();
		return ($rs)? $rs[0] : false;
	}
	function updateStatus($id, $status){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('payment_status', $status);
		return $this->CI->db->where('id', $id)->update(TBL_PAYMENT_HISTORY);
	}
	//end class
}	